<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Script-Type" content="text/css" />
<title>修改标准</title>
<link type="text/css" href="/css/tablecloth.css" rel="stylesheet" media="screen" />
<script type="text/javascript" src="/js/jquery-1.4.4.js"></script>
<script type="text/javascript" src="/js/tablecloth.js" ></script>
<script type="text/javascript" src="/js/tableinput.js"></script>
<script type="text/javascript">

function isDigit(s) 
{ 
	var patrn=/^\d+(\.\d+)?$/; 
	//var patrn = /^[0-9]{1,20}$/;
	if (!patrn.exec(s)) return false ;
	return true ;
}

function addStandard()
{
	var form = document.getElementById("addStandardForm");
	form.nutrimentID.value = 
		$("#nutriments").val();
	form.classificationID.value = 
		$("#classifications").val();
	form.max.value = 
		$("#maxInput").text();
	form.min.value = 
		$("#minInput").text();
	
	if(!(isDigit(form.max.value)&&isDigit(form.min.value)&&form.max.value>form.min.value))
	{
		alert("输入数据错误，请重新出入");
		return;
	}
	
	form.submit();
	alert("添加成功");
}

function removeStandard(id)
{
	var form = document.getElementById("removeStandardForm");
	form.standardID.value = id;
	alert("删除成功");
	form.submit();
}

$(document).ready(function() {
	$("#nutriments").change(function(){
		var id = $("#nutriments").val();
		$.post("/nutriment/findByIdToJson.do", {id:id}, function(data){
			$("#unit").html(data.unit);
		}, "json");
	});
	$("#nutriments").change();
});
</script>
</head>

<body>
	<form action = "/standard/addStandard.do" method="post" id="addStandardForm">
		<input type="hidden" name="nutrimentID" />
		<input type="hidden" name="classificationID" />
		<input type="hidden" name="max" />
		<input type="hidden" name="min" />
	</form>	
	<form action="/standard/removeStandard.do" method="post" id="removeStandardForm">
		<input type="hidden" name="standardID" />
	</form>
	<table id="t" cellspacing="0" cellpadding="0">
		<tr>
			<th class='title1'>营养素</th>
			<th class='title2'>单位</th>
			<th class='title3'>标准</th>
			<th class='title4'>MIN</th>
			<th class='title5'>MAX</th>
			<th class='title6'>添加/删除</th>
		</tr>
		<%@ taglib uri="/WEB-INF/tld/list-taglib.tld" prefix="tj" %>
		<tr>
			<td><tj:list id="nutriments" list="${nutriments}"></tj:list></td>
			<td id="unit">-</td>
			<td><tj:list id="classifications" list="${classifications}"></tj:list></td>
			<% out.print("<td id='minInput' needInput='true'>单击添加</td>"); %>
			<% out.print("<td id='maxInput' needInput='true'>单击添加</td>"); %>
			<td><input type='button' value='添加' onclick = 'addStandard()' /></td>
		</tr>
		<%@ page import="db.standard.Standard" %>
		<%@ page import="java.util.List" %>
		<%
			@SuppressWarnings("unchecked")
			List<Standard> standards = (List<Standard>)request.getAttribute("standards");
			for(int i=0; i<standards.size();i++)
			{
				out.print("<tr>");
				if(standards.get(i).getNutriment() == null)
				{
					out.print("<td>无营养素</td>");
					out.print("<td>-</td>");
				}
				else
				{
					out.print("<td>" + standards.get(i).getNutriment().getName() + "</td>");
					out.print("<td>" + standards.get(i).getNutriment().getUnit() + "</td>");
				}
				if(standards.get(i).getClassification() == null)
				{
					out.print("<td>无标准</td>");
				}
				else
				{
					out.print("<td>"+standards.get(i).getClassification().getName()+"</td>");
				}
				out.print("<td>" + standards.get(i).getMinInUnit() + "</td>");
				out.print("<td>" + standards.get(i).getMaxInUnit() + "</td>");
				out.print("<td><input type='button' value='删除' onclick=removeStandard(" + standards.get(i).getId() + ") /></td>");
				out.print("</tr>");
			}
		%>						
	</table>
	<p>&nbsp;</p>
</body>
</html>
